local anzhuo = fk.CreateSkill{
  name = "steam__anzhuo",
}

Fk:loadTranslationTable{
  ["steam__anzhuo"] = "按啄",
  [":steam__anzhuo"] = "你可为攻击范围内的角色响应或使用【杀】，其须交给你一张牌或令你获得一张【影】。每轮结束后，你重铸所有非【杀】牌来令额定摸牌数，攻击范围，出杀次数均+1",
  ["#steam__anzhuo"] = "按啄：你可为攻击范围内的角色响应或使用【杀】",
  ["#steam__anzhuo-choice"] = "按啄：你可为攻击范围内的角色响应或使用【杀】",
  ["#steam__anzhuo-give"] = "按啄：请交给 %dest 一张牌",
  ["#steam__anzhuo-give-shadow"] = "按啄：请令 %dest 获得一张【影】",

  ["@steam__anzhuo_bonus"] = "按啄",


}
local U = require "packages/utility/utility"

-- 为攻击范围内的角色响应或使用【杀】
anzhuo:addEffect(fk.AskForCardUse,{
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
      return  target ~= player and target:inMyAttackRange(player) and player:hasSkill(self) and  
      Exppattern:Parse(data.pattern):matchExp("slash")
     
  end,
  on_cost = function(self, event, target, player, data)
    local room=player.room
    local respond = room:askToCards(player, {
      min_num=1,
      max_num=1,
      skill_name = anzhuo.name,
      pattern = "slash",
      prompt = "#steam__anzhuo-choice::"..target.id,
      cancelable = true,
    })
    if #respond > 0 then
      event:setCostData(self, {card = respond[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
      local room=player.room
      local skillName=anzhuo.name
      local name="slash"
      local subcard=event:getCostData(self).card
      local card = Fk:cloneCard(name)
      card.skillName = skillName
      card:addSubcard(subcard)
      data.result = {
        from = target,
        card = card,
      }

      --交给你一张牌或影
      local card = room:askToCards(target, {
        skill_name = anzhuo.name,
        min_num = 1,
        max_num = 1,
        include_equip = false,
        pattern = ".|.|.|hand",
        prompt = "#steam__anzhuo-give::"..player.id,
        cancelable = true,
      })
      if #card > 0 then
        room:obtainCard(player, getcard, true, fk.ReasonGive, player, anzhuo.name)

      else
        room:moveCards({
          ids = U.getShade(room, 1),
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = anzhuo.name,
          moveVisible = true,
        })
      end      

  end,
})

-- 为攻击范围内的角色响应或使用【杀】
anzhuo:addEffect(fk.AskForCardResponse,{
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
      return  target ~= player and player:hasSkill(self) and target:inMyAttackRange(player) and  
      Exppattern:Parse(data.pattern):matchExp("slash")
     
  end,
  on_cost = function(self, event, target, player, data)
    local room=player.room
    local respond = room:askToCards(player, {
      min_num=1,
      max_num=1,
      skill_name = anzhuo.name,
      pattern = "slash",
      prompt = "#steam__anzhuo-choice::"..target.id,
      cancelable = true,
    })
    if #respond > 0 then
      event:setCostData(self, {card = respond[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
      local room=player.room
      local skillName=anzhuo.name
      local name="slash"
      local subcard=event:getCostData(self).card
      local card = Fk:cloneCard(name)
      card.skillName = skillName
      card:addSubcard(subcard)
      data.result = {
        from = target,
        card = card,
      }


      --交给你一张牌或影
      local getcard = room:askToCards(target, {
        skill_name = anzhuo.name,
        min_num = 1,
        max_num = 1,
        include_equip = false,
        pattern = ".|.|.|hand",
        prompt = "#steam__anzhuo-give::"..player.id,
        cancelable = true,
      })
      if #getcard > 0 then
        room:obtainCard(player, getcard, true, fk.ReasonGive, player, anzhuo.name)
      else
        room:moveCards({
          ids = U.getShade(room, 1),
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonJustMove,
          proposer = player,
          skillName = anzhuo.name,
          moveVisible = true,
        })
      end


  end,
})

-- 每轮结束后重铸所有非【杀】牌
anzhuo:addEffect(fk.RoundEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name)
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local hand_cards = player:getCardIds("h")
    local non_slash_cards = {}
    
    -- 找出所有非【杀】牌
    for _, card_id in ipairs(hand_cards) do
      local card = Fk:getCardById(card_id)
      if card and card.trueName ~= "slash" then
        table.insert(non_slash_cards, card_id)
      end
    end
    
    if #non_slash_cards > 0 then
      -- 重铸所有非【杀】牌
      room:recastCard(non_slash_cards, player, self.name)
      
      -- 增加额定摸牌数、攻击范围、出杀次数
      local mark_name = "@steam__anzhuo_bonus"
      local current_bonus = player:getMark(mark_name) or 0
      room:setPlayerMark(player, mark_name, current_bonus + 1)
    end
  end,
})

-- 增加摸牌数
anzhuo:addEffect(fk.DrawNCards, {
  anim_type = "drawcard",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and data.n > 0 and player:getMark("@steam__anzhuo_bonus")>0
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local bonus = player:getMark("@steam__anzhuo_bonus") or 0
    data.n = data.n + bonus
  end,
})

anzhuo:addEffect("atkrange", {
  correct_func = function(self, from, to)
    if from:hasSkill(anzhuo.name) and from:getMark("@steam__anzhuo_bonus")>0 then
      return from:getMark("@steam__anzhuo_bonus")
    end
  end,
})

anzhuo:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope, card, to)
    if player:hasSkill(anzhuo.name) and card.trueName == "slash" then
      return player:getMark("@steam__anzhuo_bonus")
    end
  end,
})

return anzhuo 